<div class="content">
  <h1>Space</h1>

  <p>Database Size: <%= @database_size %></p>

  <% if @system_stats_enabled %>
    <div id="chart-1" class="chart">Loading...</div>
    <%= javascript_tag nonce: true do %>
      new Chartkick.LineChart("chart-1", <%= pghero_js_value(free_space_stats_path) %>, {colors: ["#5bc0de"], bytes: true, library: {plugins: {tooltip: {intersect: false, mode: "index"}}}})
    <% end %>
  <% end %>

  <!--
  <% if @index_bloat.any? %>
    <p>Check out <%= link_to "index bloat", index_bloat_path %> for an easy way to reclaim space.</p>
  <% end %>
  -->

  <% if @unused_indexes.any? %>
    <p>
      <%= pluralize(@unused_indexes.size, "unused index") %>. Remove them
      <% if @show_migrations %>
        <a href="#" class="migration-link">with a migration</a>
      <% end %>
      for faster writes.

      <% if @database.replicating? %>
        Check they aren’t used on replicas.
      <% end %>
    </p>

    <div class="migration">
      <pre>rails generate migration remove_unused_indexes</pre>
      <p>And paste</p>
      <pre><% @unused_indexes.sort_by { |q| [-q[:size_bytes], q[:index]] }.each do |query| %>
<%= pghero_remove_index(query) %><% end %></pre>
    </div>
  <% end %>

  <% if @sizes_timeout %>
    <p>Breakdown not available (system catalog locked)</p>
  <% else %>
    <table class="table space-table">
      <thead>
        <tr>
          <th><%= link_to (@only_tables ? "Table" : "Relation"), @header_options.merge(sort: "name") %></th>
          <th class="width-15"><%= link_to "Size", @header_options %></th>
          <% if @space_stats_enabled %>
            <th class="width-15"><%= link_to "#{@days}d Growth", @header_options.merge(sort: "growth") %></th>
          <% end %>
        </tr>
      </thead>
      <tbody>
        <% @relation_sizes.each do |query| %>
          <tr>
            <td class="<%= query[:type] == "index" ? "space-index" : "" %>">
              <span class="break-all">
                <% name = query[:relation] || query[:table] %>
                <% if @space_stats_enabled %>
                  <%= link_to name, relation_space_path(name, schema: query[:schema]), target: "_blank", class: "relation-link" %>
                <% else %>
                  <%= name %>
                <% end %>
              </span>
              <% if query[:schema] != "public" %>
                <span class="text-muted"><%= query[:schema] %></span>
              <% end %>
              <% if @unused_index_names.include?(query[:relation]) %>
                <span class="unused-index">UNUSED</span>
              <% end %>
            </td>
            <td><%= query[:size] %></td>
            <% if @space_stats_enabled %>
              <td>
                <% if @growth_bytes_by_relation[[query[:schema], query[:relation]]] %>
                  <% if @growth_bytes_by_relation[[query[:schema], query[:relation]]] < 0 %>-<% end %><%= PgHero.pretty_size(@growth_bytes_by_relation[[query[:schema], query[:relation]]].abs) %>
                <% else %>
                  <span class="text-muted">Unknown</span>
                <% end %>
              </td>
            <% end %>
          </tr>
        <% end %>
      </tbody>
    </table>
  <% end %>
</div>
